// AdminController.java
package swb.api.com.api;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import swb.api.com.repository.dto.*;
import swb.api.com.service.*;
import swb.api.com.vo.req.AdminCreateRequest;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/admin")
public class AdminController {

    @Autowired
    private UserService userService;

    @Autowired
    private WithdrawAccountService withdrawAccountService;

    @Autowired
    private WithdrawRecordService withdrawRecordService;

    @Autowired
    private SysMenuService sysMenuService;

    @Autowired
    private SysRoleService sysRoleService;

    // 用户管理接口
    @GetMapping("/users")
    public ResponseEntity<Page<UserInfo>> getUserList(
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size) {
        Pageable pageable = PageRequest.of(page, size);
        Page<UserInfo> users = userService.findAll(pageable);
        return ResponseEntity.ok(users);
    }

    @PutMapping("/users/{id}/balance")
    public ResponseEntity<String> updateUserBalance(
            @PathVariable Long id,
            @RequestBody Map<String, Object> params) {
        BigDecimal newBalance = new BigDecimal(params.get("balance").toString());
        userService.updateBalance(id, newBalance);
        return ResponseEntity.ok("更新成功");
    }

    // 提现账户管理接口
    @GetMapping("/withdraw/accounts")
    public ResponseEntity<Page<WithdrawAccount>> getWithdrawAccounts(
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size) {
        Pageable pageable = PageRequest.of(page, size);
        Page<WithdrawAccount> accounts = withdrawAccountService.findAll(pageable);
        return ResponseEntity.ok(accounts);
    }

    @PutMapping("/withdraw/accounts/{id}/status")
    public ResponseEntity<String> updateAccountStatus(
            @PathVariable Long id,
            @RequestBody Map<String, Object> params) {
        Integer status = Integer.parseInt(params.get("status").toString());
        withdrawAccountService.updateStatus(id, status);
        return ResponseEntity.ok("更新成功");
    }

    // 提现记录管理接口
    @GetMapping("/withdraw/records")
    public ResponseEntity<Page<WithdrawRecord>> getWithdrawRecords(
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(required = false) Integer status) {
        Pageable pageable = PageRequest.of(page, size);
        Page<WithdrawRecord> records = withdrawRecordService.findByStatus(status, pageable);
        return ResponseEntity.ok(records);
    }

    @PutMapping("/withdraw/records/{id}/process")
    public ResponseEntity<String> processWithdraw(
            @PathVariable Long id,
            @RequestBody Map<String, Object> params) {
        Integer status = Integer.parseInt(params.get("status").toString());
        String remark = params.get("remark").toString();
        withdrawRecordService.processWithdraw(id, status, remark);
        return ResponseEntity.ok("处理成功");
    }

    // 菜单管理接口
    @GetMapping("/menus")
    public ResponseEntity<List<SysMenu>> getMenuTree() {
        List<SysMenu> menus = sysMenuService.getMenuTree();
        return ResponseEntity.ok(menus);
    }

    @PostMapping("/menus")
    public ResponseEntity<String> addMenu(@RequestBody SysMenu menu) {
        sysMenuService.save(menu);
        return ResponseEntity.ok("添加成功");
    }

    @PutMapping("/menus/{id}")
    public ResponseEntity<String> updateMenu(
            @PathVariable Long id,
            @RequestBody SysMenu menu) {
        menu.setId(id);
        sysMenuService.update(menu);
        return ResponseEntity.ok("更新成功");
    }

    @DeleteMapping("/menus/{id}")
    public ResponseEntity<String> deleteMenu(@PathVariable Long id) {
        sysMenuService.delete(id);
        return ResponseEntity.ok("删除成功");
    }

    // 角色管理接口
    @GetMapping("/roles")
    public ResponseEntity<Page<SysRole>> getRoles(
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size) {
        Pageable pageable = PageRequest.of(page, size);
        Page<SysRole> roles = sysRoleService.findAll(pageable);
        return ResponseEntity.ok(roles);
    }

    @PostMapping("/roles")
    public ResponseEntity<String> addRole(@RequestBody SysRole role) {
        sysRoleService.save(role);
        return ResponseEntity.ok("添加成功");
    }

    @GetMapping("/roles/{id}/menus")
    public ResponseEntity<List<Long>> getRoleMenus(@PathVariable Long id) {
        List<Long> menuIds = sysRoleService.getMenuIdsByRoleId(id);
        return ResponseEntity.ok(menuIds);
    }

    @PostMapping("/roles/{id}/menus")
    public ResponseEntity<String> assignRoleMenus(
            @PathVariable Long id,
            @RequestBody List<Long> menuIds) {
        sysRoleService.assignMenus(id, menuIds);
        return ResponseEntity.ok("分配成功");
    }

    // 管理员账户管理接口
    @PreAuthorize("hasRole('ADMIN')")
    @PostMapping("/admins")
    public ResponseEntity<String> createAdmin(@RequestBody AdminCreateRequest adminCreateRequest) {
        try {
            userService.createAdmin(adminCreateRequest);
            return ResponseEntity.ok("管理员账户创建成功");
        } catch (Exception e) {
            return ResponseEntity.badRequest().body("创建失败: " + e.getMessage());
        }
    }
}
